<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.simpleyql.*" %>
<%@ page import="com.dhpollack.rotogss.data.RotoGSSUsers" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="javax.jdo.JDOObjectNotFoundException" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.dhpollack.rotogss.helper.PMF" %>


<html>
  <head>
  <meta name="google-site-verification" content="jyuD8pxqyHNanYQmn8LpSiWSopnDBt1Qk1a5QGYznFc" />
  </head>
  <body>
  <h2>Welcome to RotoGSS</h2>
  <br /><br />

<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    
    
    if (user != null) {
        Key userkey = KeyFactory.createKey(RotoGSSUsers.class.getSimpleName(),user.getNickname());
		PersistenceManager pm = PMF.get().getPersistenceManager();
        RotoGSSUsers rotouser = null;
		try {
			rotouser = pm.getObjectById(RotoGSSUsers.class, userkey);
			if(rotouser.getYahooOAuthData() == null) {
%>
				<a href="/yahooauthstart">Authorize</a> This Application With Yahoo
<%
			} else {
%>
    		You have already authorized this application with Yahoo. 
    		<a href="/yahooauthstart">Reauthorize Here</a>
<%
			}
%>
			<br />
<%
		} catch(JDOObjectNotFoundException e) {
%>
			<a href="/yahooauthstart">Authorize</a> This Application With Yahoo
			<br />
			<a href="/googleauthstart">Authorize</a> This Application With Google
			
<%
		} catch(NullPointerException e) {
%>
			<a href="/yahooauthstart">Authorize</a> This Application With Yahoo
<%
		}
		
		try {
			if(rotouser.getGoogleAccessToken() == null) {
%>
				<a href="/googleauthstart">Authorize</a> This Application With Google
<%
			} else {
%>
	    		You have already authorized this application with Google. 
    			<a href="/googleauthstart">Reauthorize Here</a>
<%
			}
		} catch(NullPointerException e) {
%>
			<a href="/googleauthstart">Authorize</a> This Application With Google
			
<%
		}
%>
<br />
<p>Hello, <%= user.getNickname() %>
<br />
(You can <a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)
<br />
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to Google.</p>
<%
    }
%>

  </body>
</html>